// pages/home/home.js
import {
  HTTP
} from '../../utils/http'
let http = new HTTP()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    value: '搜索',
    workList: [],
    boxList: [],
    searchPanel: false,
    workPageNum: 1,
    workPageSize: 10,
    boxPageNum: 1,
    boxPageSize: 10,
    scrollH: 0,
    workTotal: 0,
    boxTotal: 0,
    tabActiveTitle: "创作盒"
  },
  onSearch(e) {
    wx.navigateTo({
      url: "/pages/search-box/search-box"
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    
  },
  getBoxList() {
    http.request({
      url: '/ai/draw/box/get/page',
      method: 'post',
      data: {
        "pageSize": this.data.boxPageSize,
        "pageNum": this.data.boxPageNum
      },
      success: (res) => {
        if (res.code === "0") {
          let list = res.data.list
          var arr1 = this.data.boxList;
          var arr2 = list;
          arr1 = arr1.concat(arr2);
          this.setData({
            boxList: arr1,
            boxTotal: res.data.total
          })
        }
      }
    })
  },
  getWorkList() {
    http.request({
      url: '/ai/draw/picture/get/page',
      method: 'post',
      data: {
        "pageSize": this.data.workPageSize,
        "pageNum": this.data.workPageNum
      },
      success: (res) => {
        if (res.code === "0") {
          let list = res.data.list
          var arr1 = this.data.workList;
          var arr2 = list;
          arr1 = arr1.concat(arr2);
          this.setData({
            workList: arr1,
            workTotal: res.data.total
          })
        }
      }
    })
  },
  onTabChange(e) {
    this.setData({
      tabActiveTitle: e.detail.title
    })
    if (e.detail.title === "创作盒") {
      this.setData({
        boxList: [],
        boxPageNum: 1,
        boxPageSize: 10,
        boxTotal: 0
      })
      this.getBoxList()
    } else if (e.detail.title === "作品") {
      this.setData({
        workList: [],
        workPageNum: 1,
        workPageSize: 10,
        workTotal: 0
      })
      this.getWorkList()
    }

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getBoxList();
    this.getWorkList();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {
    this.setData({
      workList: [],
      boxList: [],
      workPageNum: 1,
      workPageSize: 10,
      boxPageNum: 1,
      boxPageSize: 10
    })
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function (event) {
    if (this.data.tabActiveTitle === "作品") {
      if (!(this.data.workList && this.data.workList.length > 0)) return
      let currentTotal = this.data.workList.length;
      if (currentTotal <= this.data.workTotal) {
        var current = this.data.workPageNum * 1 + 1;
        this.setData({
          workPageNum: current
        })
        this.getWorkList();
      }
    } else if (this.data.tabActiveTitle === "创作盒") {
      if (!(this.data.boxList && this.data.boxList.length > 0)) return
      let currentTotal = this.data.boxList.length;
      if (currentTotal <= this.data.boxTotal) {
        var current = this.data.boxPageNum * 1 + 1;
        this.setData({
          boxPageNum: current
        })
        this.getBoxList();
      }
    }

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})